//
//  PKReadDetailListTableViewController.m
//  PianKe
//
//  Created by lanou on 15/12/29.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKReadDetailListTableViewController.h"
#import "PKReadDetailTableViewCell.h"
#import "PKReadDetailModel.h"
@interface PKReadDetailListTableViewController ()


// 数据源数组
@property (strong, nonatomic) NSMutableArray *listArray;

// 设置初始请求位置
@property (assign, nonatomic) NSInteger requestStart;

@end

@implementation PKReadDetailListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _requestStart = 0;
    _listArray = [NSMutableArray array];
    
    [self setUpData];
    
    // 注册cell
    [self.tableView registerClass:[PKReadDetailTableViewCell class] forCellReuseIdentifier:@"MyCell"];
    self.tableView.rowHeight = 150;
    
    
    
#pragma mark  ----- 上啦加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        _requestStart += 10;
        
        [self setUpData];
        
    }];
    
#pragma mark ----- 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        _requestStart = 0;
        
        [self setUpData];
        
    }];
    
    
    
}




- (void)setUpData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"typeid"] = _typeID;
    parDic[@"client"] = @"1";
    parDic[@"sort"] = _sortType;
    parDic[@"limit"] = @"10";
    parDic[@"version"] = @"3.0.2";
    parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
    parDic[@"start"] = [NSString stringWithFormat:@"%ld",_requestStart];
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/read/columns_detail" parDic:parDic Method:POST finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = dic[@"data"];
        NSArray *array = dataDic[@"list"];
        
        
        if (_requestStart == 0) {
            
            [_listArray removeAllObjects];
            
        }
        
        
        for (NSDictionary *oneDic in array) {
            PKReadDetailModel *model = [[PKReadDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            
            [_listArray addObject:model];
            
        }
        
#pragma mark ----- 取消加载
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        // 刷新
        [self.tableView reloadData];
        
        
        
    } error:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _listArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKReadDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    
    
    // 展示数据
    PKReadDetailModel *model = _listArray[indexPath.row];
    cell.model = model;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"sadasd ");
    
    PKReadDetailModel *model = _listArray[indexPath.row];
    
    NSDictionary *dic = @{
                          @"url":model.ID
                          };
    
    // 发送通知,告诉DetailVC去跳转
    // 因为两个tableView但不是DetailVC管理的子视图,(没有继承到 导航栏),所以不具备push的能力
    // 所以我们发送通知,让DetailVC去跳转
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BBB" object:self userInfo:dic];

}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
